/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.hk.sys.web.user;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.hk.sys.entity.user.HkUserGoods;
import com.thinkgem.jeesite.modules.hk.sys.service.user.HkUserGoodsService;

/**
 * 用户物品管理Controller
 * @author 罗天文
 * @version 2018-03-14
 */
@Controller
@RequestMapping(value = "${adminPath}/sys/user/hkUserGoods")
public class HkUserGoodsController extends BaseController {

	@Autowired
	private HkUserGoodsService hkUserGoodsService;
	
	@ModelAttribute
	public HkUserGoods get(@RequestParam(required=false) String id) {
		HkUserGoods entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = hkUserGoodsService.get(id);
		}
		if (entity == null){
			entity = new HkUserGoods();
		}
		return entity;
	}
	
	@RequiresPermissions("sys:user:hkUserGoods:view")
	@RequestMapping(value = {"list", ""})
	public String list(HkUserGoods hkUserGoods, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<HkUserGoods> page = hkUserGoodsService.findPage(new Page<HkUserGoods>(request, response), hkUserGoods); 
		model.addAttribute("page", page);
		return "hk/sys/user/hkUserGoodsList";
	}

	@RequiresPermissions("sys:user:hkUserGoods:view")
	@RequestMapping(value = "form")
	public String form(HkUserGoods hkUserGoods, Model model) {
		model.addAttribute("hkUserGoods", hkUserGoods);
		return "hk/sys/user/hkUserGoodsForm";
	}

	@RequiresPermissions("sys:user:hkUserGoods:edit")
	@RequestMapping(value = "save")
	public String save(HkUserGoods hkUserGoods, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, hkUserGoods)){
			return form(hkUserGoods, model);
		}
		hkUserGoodsService.save(hkUserGoods);
		addMessage(redirectAttributes, "保存用户物品成功");
		return "redirect:"+Global.getAdminPath()+"/sys/user/hkUserGoods/?repage";
	}
	
	@RequiresPermissions("sys:user:hkUserGoods:edit")
	@RequestMapping(value = "delete")
	public String delete(HkUserGoods hkUserGoods, RedirectAttributes redirectAttributes) {
		hkUserGoodsService.delete(hkUserGoods);
		addMessage(redirectAttributes, "删除用户物品成功");
		return "redirect:"+Global.getAdminPath()+"/sys/user/hkUserGoods/?repage";
	}

}